//
//  StorageEngine.m
//  MobileOA
//
//  Created by Woo Jack on 13-6-27.
//  Copyright (c) 2013年 chenxiaojuan. All rights reserved.
//

#import "StorageEngine.h"
#import "ServerEngine.h"

@implementation StorageEngine

+(BOOL)checkPath:(NSString*)path
{
    BOOL isDir = NO;
	NSFileManager* fm = [NSFileManager defaultManager];
	BOOL ret = [fm fileExistsAtPath:path isDirectory:&isDir];
	return ret || isDir;
}

+(BOOL)createPath:(NSString *)path
{
    NSFileManager* fm = [NSFileManager defaultManager];
	return [fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}

+(NSString*)readFile:(NSString *)filename
{
    return [NSString stringWithContentsOfFile:filename
                                     encoding:NSUTF8StringEncoding
                                        error:nil];
}

+(BOOL)writeFile:(NSString *)filename content:(NSString *)content
{
    return [content writeToFile:filename
                      atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

+(BOOL)removeFile:(NSString *)filename
{
    NSFileManager* fm = [NSFileManager defaultManager];
    return [fm removeItemAtPath:filename error:nil];
}

+(BOOL)downloadNetFile:(NSString *)strOfURL
{
    
    NSURL    *URL = [NSURL URLWithString:strOfURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    
    /* 下载的数据 */
    if (data != nil)
    {
        NSLog(@"下载成功");
        NSString* filename =[[NSString stringWithFormat:@"%@",strOfURL] md5Hash];
        NSString *filePath = [NSString stringWithFormat:@"%@%@",DE.mainPath,filename];

        
        if ([data writeToFile:filePath atomically:YES])
        {
            NSLog(@"保存成功.");
            return YES;

        }
        else
        {
            NSLog(@"保存失败.");
            return NO;

        }
    }
    else
    {
        NSLog(@"%@", error);
        return NO;
    }
    return NO;

}

+(NSString*)readloadNetFile:(NSString *)strOfURL{
    
    NSString* filename =[[NSString stringWithFormat:@"%@",strOfURL] md5Hash];
    NSString *filePath = [NSString stringWithFormat:@"%@%@",DE.mainPath,filename];
    return filePath;
    
}


@end
